//
//  RadioHelpLisViewController.m
//  RainbowFM
//
//  Created by Kratos on 14/12/11.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "RadioHelpLisViewController.h"
#import "RadioHelpIntroducedViewController.h"

#import "KKSwitchBarView.h"
#import "RadioHelpListTableViewCell.h"

#import "HelpModel.h"

@interface RadioHelpLisViewController ()<UITableViewDataSource,UITableViewDelegate,KKSwitchBarViewDelegate>{
    NSMutableArray *listArray;
}
@property (nonatomic , weak) IBOutlet KKSwitchBarView *switchBarView;
@property (nonatomic , weak) IBOutlet UITableView *tableview;

@property (nonatomic , strong) NSMutableDictionary *helpListDic;
@property (nonatomic , assign) NSInteger itemIndex;

@property (nonatomic , assign) BOOL leftBarShow;
@end

@implementation RadioHelpLisViewController
-(void)awakeFromNib
{
    _helpListDic = [NSMutableDictionary dictionary];
    [_helpListDic setObject:@[] forKey:@"0"];
    [_helpListDic setObject:@[] forKey:@"1"];
    [_helpListDic setObject:@[] forKey:@"2"];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    listArray = [NSMutableArray array];
    if (!_leftBarShow) {
        self.navigationItem.leftBarButtonItem = nil;
    }
    
    [self initializeTopView];

    [self updateHelpList:0];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setHeftBarButtonItemShow:(BOOL)flag
{
    _leftBarShow = flag;
}



//初始化table
-(void)initializeTopView
{
    FontSizeButtonObject *btnSetInfo0 = [FontSizeButtonObject new];
    btnSetInfo0.buttonDefaultImage = @"help_topbar_button1_n";
    btnSetInfo0.buttonSelectedImage = @"help_topbar_button1_p";
    
    FontSizeButtonObject *btnSetInfo1 = [FontSizeButtonObject new];
    btnSetInfo1.buttonDefaultImage = @"help_topbar_button2_n";
    btnSetInfo1.buttonSelectedImage = @"help_topbar_button2_p";
    
    FontSizeButtonObject *btnSetInfo2 = [FontSizeButtonObject new];
    btnSetInfo2.buttonDefaultImage = @"help_topbar_button3_n";
    btnSetInfo2.buttonSelectedImage = @"help_topbar_button3_p";
    
    CGRect frame = _switchBarView.frame;
    frame.size.width = CGRectGetWidth(self.view.bounds);
    _switchBarView.frame = frame;
    
    [_switchBarView setFontSizeBtns:@[btnSetInfo0,btnSetInfo1,btnSetInfo2] selectedIndex:0];
    _itemIndex = 0;
    
}

-(void)updateHelpList:(NSInteger)index
{
    index = index+1;
    [SVProgressHUD show];
    
    typeof(self) self_ = self;
    [listArray removeAllObjects];
    if (index == 1) {
        index = 2;
    }else if(index == 2){
        index = 1;
    }
    [[HelpModel sharedModel] fetchHelpListWithType:_F(@"%d",index) currentPage:1 pageSize:200 result:^(NSArray *list, NSString *msg) {
        
        [SVProgressHUD dismiss];
        [listArray addObjectsFromArray:list];
//        [self_.helpListDic setObject:list forKey:_F(@"%d",index)];
        [self_.tableview reloadData];
        
    }];
}


#pragma mark - table datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSArray *list = _helpListDic[_F(@"%d",_itemIndex)];
//    return [list count];
    
    return listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RadioHelpListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RadioHelpListTableViewCell"];

//    NSArray *list = _helpListDic[_F(@"%d",_itemIndex)];
    [cell updateContents:listArray[indexPath.row]];
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    RadioHelpListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RadioHelpListTableViewCell"];
    
//    NSArray *list = _helpListDic[_F(@"%d",_itemIndex)];
    [cell updateContents:listArray[indexPath.row]];
    [cell layoutIfNeeded];
    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    if (height < 108) {
        height = 108;
    }
    return height+1;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}


#pragma mark - table delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - KKSwitchBarViewDelegate
-(void)switchBarSelectedIndex:(NSUInteger)index switchBar:(KKSwitchBarView *)switchBar
{
    if (_itemIndex == index) {
        return;
    }
    
    _itemIndex = index;
    [self updateHelpList:_itemIndex];
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if ([sender isKindOfClass:[RadioHelpListTableViewCell class]]) {
        NSIndexPath *indexPath = [_tableview indexPathForCell:sender];
        
        NSArray *list = _helpListDic[_F(@"%d",_itemIndex)];
        
        HelpItem *item = list[indexPath.row];
        
        NSString *title = @"电台帮";
        
        RadioHelpIntroducedViewController *radioHelpIntroducedViewController = [segue destinationViewController];
        radioHelpIntroducedViewController.item = item;
        [radioHelpIntroducedViewController setTitle:title];
    }
    
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
